package com.uum.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.admin.pojo.entity.SysLoginLog;
import com.uum.admin.mapper.SysLoginLogMapper;
import com.uum.admin.pojo.query.LoginLogPageQuery;
import com.uum.admin.pojo.vo.LoginLogPageVO;
import com.uum.admin.service.SysLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public Page<LoginLogPageVO> page(LoginLogPageQuery logPageQuery) {
        String location = logPageQuery.getLocation();
        String ip = logPageQuery.getIp();
        String client = logPageQuery.getClient();
        String username = logPageQuery.getUsername();
        String startTime = logPageQuery.getStartTime();
        String endTime = logPageQuery.getEndTime();
        Page page = new Page<>(logPageQuery.getCurrentPage(), logPageQuery.getPageSize());
        QueryWrapper<LoginLogPageVO> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(ip)) {
            queryWrapper.like("t1.ip",ip);
        }
        if (StrUtil.isNotBlank(location)) {
            queryWrapper.like("t1.location",location);
        }
        if (StrUtil.isNotBlank(username)) {
            queryWrapper.like("t2.username",username);
        }
        if (StrUtil.isNotBlank(client)) {
            queryWrapper.eq("t1.client",client);
        }
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            queryWrapper.between("t1.login_time",startTime,endTime);
        }
        queryWrapper.orderByDesc("t1.login_time");
        List<LoginLogPageVO> result = baseMapper.page(page, queryWrapper);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<Map<String, Object>> getUVByWeek(String startTime, String endTime) {
        return baseMapper.getUVByWeek(startTime,endTime);
    }

}
